package com.kejizhentan.service.impl;

import com.kejizhentan.dao.IUserInforDao;
import com.kejizhentan.service.IUserService;
import com.kejizhetan.bean.Role;
import com.kejizhetan.bean.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
@Transactional
public class UserService implements IUserService {
    @Autowired
    private IUserInforDao userInforDao;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = null;
        try {
            userInfo = userInforDao.findUserInfoByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //处理自己的user对象封装成UserDetails对象,在密码不加密的情况下，password前需要加"{noop}"
        //User user = new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),getAuthority(userInfo.getRoles()));
        //通过用户状态设置用户是否可用
//        User user = new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),userInfo.getStatus() == 0  ? false:true,
//                true,true,true,getAuthority(userInfo.getRoles()));
        //用户密码加密之后password前的"{noop}"需要去掉
        User user = new User(userInfo.getUsername(),userInfo.getPassword(),userInfo.getStatus() == 0  ? false:true,
                true,true,true,getAuthority(userInfo.getRoles()));
        return user;
    }

    //作用就是返回一个List集合，集合中装入的是角色描述
    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {

        List<SimpleGrantedAuthority> list = new ArrayList<>();
        //list.add(new SimpleGrantedAuthority("ROLE_USER"));
        for (Role role : roles) {
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }
        return list;
    }

    @Override
    public List<UserInfo> findAll() throws Exception {

        return userInforDao.findAll();
    }

    @Override
    public void save(UserInfo userInfo) throws Exception {
        //对用户的密码进行加密处理
        userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
        userInforDao.save(userInfo);
    }

    @Override
    public UserInfo findById(String id) throws Exception {
        return userInforDao.findById(id);
    }

    @Override
    public List<Role> findOtherRoles(String userid) throws Exception {
        return userInforDao.findOtherRoles(userid);
    }

    @Override
    public void addRoleToUser(String userId, String[] roleIds) throws Exception {
        for (String roleId : roleIds) {
            userInforDao.addRoleToUser(userId, roleId);
        }
    }
}
